package main;

import commands.Command;
import commands.CommandParser;
import observers.FileEventPublisher;
import observers.LoggerObserver;
import observers.StatsObserver;
import utils.EditorContext;

import java.util.Scanner;

public class EditorApp {
    public static void main(String[] args) {
        CommandParser parser = new CommandParser();
        try (Scanner scanner = new Scanner(System.in)) {
            FileEventPublisher.getInstance().attach(LoggerObserver.getInstance());
            FileEventPublisher.getInstance().attach(StatsObserver.getInstance());
            FileEventPublisher.getInstance().notifyObservers(null);
            while (true) {
                System.out.print("editor> ");
                if (scanner.hasNextLine()) { // Check if there's a line available
                    String rawText = scanner.nextLine().trim();
                    if (rawText.isEmpty()) {  // 检查是否为空行
                        continue;  // 如果是空行或只有空格的行，则跳过本次循环
                    }
                    try {
                        Command cmd = parser.parse(rawText);
                        if (cmd != null) {
                            cmd.execute();
                        }
                    } catch (Exception e) {
                        System.out.println("Error: " + e.getMessage());
                    }
                } else {
                    // No more input, break out of the loop
                    break;
                }
            }
        }
    }
}
